<?php
class OrderApp extends CommonApp {

	private $table = 'order';


	/**
	 * 首页
	 */
	public function indexAct(){
		//search
		$search = array();
		$search['where'] = "1 ";
		// $search['where'] .= "AND A.`item`='{$item}' ";
		//publish
		if(My::arr2var($_GET,'publish')==='0'){
			$search['where'] .= "AND publish=0 ";
		}elseif(My::arr2var($_GET,'publish')==1){
			$search['where'] .= "AND publish=1 ";
		}
		//搜索卡号
		if(My::arr2var($_GET,'orderid')!=''){
			$orderid = I('get.orderid');
			$search['where'] .= "AND `orderid` like '%{$orderid}%' ";
		}
		// //搜索id
		// if(My::is_id(My::arr2var($_GET,'member_id'))){
		// 	$member_id = $_GET['member_id'];
		// 	$search['where'] .= "AND `member_id`='{$member_id}' ";
		// }
		// //搜索姓名
		// if(My::arr2var($_GET,'username')!=''){
		// 	$username = $_GET['username'];
		// 	$search['where'] .= "AND `username` like '%{$username}%' ";
		// }
		if(isset($_GET['order'])) {
			$order = $_GET['order'];
			//排序字段为指派，正向排序为：当前用户最前，关闭最后，然后按照id正序排列
			$search['order'] = ($order=='assign') ? "`assign`={$userid} DESC, `assign`=0 ASC,`assign` DESC,`id` DESC" : $search['order'] = "`{$order}` DESC";
			//设置的字段排序与当前cookie排序一样的时候反向排序
			if(isset($_COOKIE['order']) && $search['order']==$_COOKIE['order']){
				//排序字段为指派，反向排序为：关闭最前，当前用户最后，然后按照id反序排列
				$search['order'] = ($order=='assign') ? "`assign`=0 DESC, `assign`={$userid} ASC,`assign` ASC,`editdate` DESC" : "`{$order}` ASC";
			}
			//字段排序存储cookie值一年
			setcookie('order', $search['order'], time()+31536000,'/');
			$this->ajaxReturn(200,'排序成功!');
		} else $search['order'] = isset($_COOKIE['order']) ? $_COOKIE['order'] : '`id` DESC';
		if(isset($_COOKIE['order']))
			$this->assign('order',$_COOKIE['order']);
		$search['page'] = My::arr2var($_REQUEST,'page').(isset($_SESSION['pagesize'])?$_SESSION['pagesize']:'');//用于参与缓存标识
		$search['pagesize'] = isset($_COOKIE['pagesize']) ? $_COOKIE['pagesize'] : '';
		$result=My::getCache($search,array('pre'=>$this->table));
		if($result==''){
			//page
			$count = DB($this->table)->where($search['where'])->count();
			$result['page'] = My::page($count,array('pagesize'=>$search['pagesize']));
			//result
			$result['data'] = DB($this->table)->where($search['where'])->order($search['order'])->limit($result['page']['limits'])->select();

			$result['type'] = M('admin.category')->item2submenu('logistics',array('level'=>'1'));
			//echo DB('')->getLastSql();
			// if(is_array($result['data']))
			foreach($result['data'] as &$value){
				// $value['params'] = My::decodeJson($value['params']);
				$value['user'] = DB('members')->where("id = '{$value['member_id']}'")->getField('tel');
				$value['goods'] = DB('goods')->where("id = '{$value['goodsid']}'")->getField('image');
				$value['address'] = DB('address')->where("id = '{$value['address_id']}'")->getField('address');
			}
			My::setCache($search,$result,array('pre'=>$this->table));
		}
		$this->assign('result',$result);
		$this->display();
	}

	/**
	 * 页面显示(编辑/添加)
	 * @params $_GET['id']
	 */
	public function modifyAct(){
		//分类唯一名称
		// $item = My::arr2var($_GET,'item');
		// if(!My::is_item($item))
		// 	$this->ajaxReturn(404,"分类错误!");
		//id
		$id = I('get.id');
		$result = array();
		if(My::is_id($id)){
			//当前id文章数据
			$result = DB($this->table)->where("id={$id}")->find();
			$result['type'] = M('admin.category')->item2submenu('logistics',array('level'=>'1'));
			// $result['params'] = My::decodeJson($result['params']);
			// $result['province'] = M('Address')->getProvince($result['province']);
			// $result['city'] = M('Address')->getCity($result['city']);
			// $result['area'] = M('Address')->getArea($result['area']);
			$result['logistics'] = '';
			if(!empty($result['logistics_id'])){
				$e = new Express();
		        $result['logistics'] = $e->getLogisticsInfo($result['logistics_id']);
		    }
			$this->assign('result',$result);
		}
		//地址二级联动
		// $this->assign('province',M('Address')->getProvince());
		// $this->assign('city',M('Address')->getCity());
		// $this->assign('area',M('Address')->getArea());
		//分类id->自定义表单
		//$FORM = Form::item2FORM($item);
		//$this->assign('FORM',$FORM);
		// $this->assign('item',$item);
		// $tpl = I('get.tpl',$item);
		$this->display();
	}

	/**
	* 提交表单
	**/
	public function saveAct(){
		$id = I('post.id');
		// if(!My::is_id($_POST['logistics_type']))
		// 	$this->ajaxReturn(300,'请选择快递类型!');
		if(empty($_POST['logistics_id']))
			$this->ajaxReturn(300,'请填写物流单号!');
		// $params = My::arr2var($_POST,'params');
		$e = new Express();
		$data = $e->getLogisticsInfo($_POST['logistics_id']);
		if(!$data)
			$this->ajaxReturn(300,'查询不到物流单号，请重新填写!');
		$_POST['logistics_type'] = $data['com'];
		if(My::is_id($id)){//编辑
			/*//白名单
			if($item=='activity')
				$params = My::keepParams($params,array('table'=>$this->table,'id'=>$id,'verifyId'=>'activity'));*/
			//保存表单模型参数
			// My::setParams($this->table,$params,$id,$item);
			//dump($_POST);exit;
			$ok = DB($this->table)->clear()->save($_POST);
			if($ok)	$this->ajaxReturn(200,"编辑成功");
			else		$this->ajaxReturn(300,"编辑失败");
		}else{//添加
			$_POST['adddate'] = date("Y-m-d H:i:s");
			$_POST['author'] = My::user('username');
			$result = DB($this->table)->where("card={$card}")->find();
			if($result)
				$this->ajaxReturn(300,'存在相同卡号');
			$ok = DB($this->table)->clear()->add($_POST);
			//保存表单模型参数
			// My::setParams($this->table,$params,$ok,$item);
			if($ok>=0)	$this->ajaxReturn(201,"添加成功");
			else		$this->ajaxReturn(300,"添加失败");
		}
	}
	
	/**
	* 地址联动
	**/
	public function getAddressAct(){
    	$area = C('area');
    	if($_GET['area_name'] == 'province')
    		$result = self::getOption(M('Address')->provinceId2city($_GET['area_id']),'城市','city');
    	else if($_GET['area_name'] == 'city')
    		$result = self::getOption(M('Address')->cityId2area($_GET['area_id']),'区域','area');
        $this->ajaxReturn(200,$result);
    }

    /**
	 * 组装
	 */
	private function getOption($result,$address,$city){
		$option = "<option value='' selecct='selected'>请选择".$address."...</option>";
        if($result) {
            foreach ($result as &$value) {
                $option .= "<option  value=".$value[''.$city.'_id'].">".$value[''.$city.'_name']."</option>";
            }
        }
        return $option;
	}

	/**
	 * 异步验证
	 */
	public function verifyAct($val=''){
		$search['where'] = "1 ";
		$tab = My::arr2var($_GET,'tab');
		//搜索卡号
		if(My::arr2var($_GET,'card')!=''){
			$card = I('get.card');
			$search['where'] .= "AND `card` = '{$card}' ";
		}
		//搜索姓名
		if(My::arr2var($_GET,'name')!=''){
			$name = I('get.name');
			$search['where'] .= "AND `username` = '{$name}' ";
		}
		$ok = DB($this->table)->where($search['where'])->find();
		if($ok && $tab == 'card')
			$this->ajaxReturn(300,'存在相同卡号！');
		else if(!$ok && $tab == 'card')
			$this->ajaxReturn(200,'<span class="green">正确</span>');
		else if($ok && $tab == 'r_card')
			$this->ajaxReturn(200,'<span class="green">正确</span>');
		else if(!$ok && $tab == 'r_card')
			$this->ajaxReturn(300,'没有该推荐人的卡号！');
		else if($ok && $tab == 'r_username')
			$this->ajaxReturn(200,'<span class="green">正确</span>');
		else if(!$ok && $tab == 'r_username')
			$this->ajaxReturn(300,'推荐卡号与推荐人不一致！');
	}
 }